<?php
/**
 * Created by PhpStorm.
 * User: 
 * Date: 
 * Time: 
 */

namespace app\services\model;

use think\Config;
use think\Model;
use think\Db;

class QuestionType extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'question_type';

     /**
     * [caseList description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function typeList($userId,$reqData){
        //1查询用户角色
        $roleOb = new \app\services\model\AdminRoles();
        $role = $roleOb->roleByRoleid($reqData['roleId']);
        if(!$role){
            $ret['status'] = 2001;
            return $ret;
        }
        $roleName = $role['role'];
        // $config = Config::get('api')['roleCase'];
        // // var_dump($config);exit;
        // $info = $config[$roleName];
        // if(!$info){
        //     $ret['status'] = 3001;
        //     return $ret;
        // }
        // echo $info;exit;
        $object = Db::table("question_type");
       
        if(isset($reqData['type']) && $reqData['type']!=""){
            $type = $reqData['type'];
            $object = $object->where("type like '%$type%' or instruction like '%$type%'");
        } 

        if(isset($reqData['page'])&&$reqData['page']){
            $page = $reqData['page'];
        }
        else{
            $page = 1;
        }
        $info = $object
            ->field([
                "id",
                "type",
                "instruction",
            ])
            ->paginate($reqData['pageSize'], false, ['page '=>$page]);
            // echo $object->getLastSql();exit;
        $ret['status'] = 0;
        $ret['info'] = $info;
        return $ret;
    }    
    /**
     * [typeListAll description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function typeListAll($userId){
        $object = Db::table("question_type");
       
        if(!empty($reqData['type'])){
            $type = $reqData['type'];
            $object = $object->where("type like '%$type%' or instruction like '%$type%'");
        } 

        $info = $object
            ->field([
                "id",
                "type",
                "instruction",
            ])
            ->select();
            // echo $object->getLastSql();exit;
        return $info;
    }    


    /**
     * [addType description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function addType($userId,$reqData){
        try{
            if(empty($reqData['id'])){
                //新增  
                $new['type'] = $reqData['type'];
                $new['instruction'] = $reqData['instruction'];
                Db::table("question_type")->insert($new);
                return 0;
            }
            else{
                //编辑            
                $object = Db::table("question_type")
                    ->where("id",$reqData['id'])
                    ->find();
                if(empty($object['id'])){
                    return 3008;
                }
                $new['type'] = $reqData['type'];
                $new['instruction'] = $reqData['instruction'];
                Db::table("question_type")->where("id",$reqData['id'])->update($new);
                return 0;
            } 
        }
        catch(\Exception $e){
            return 3009;
        }
    }   

    /**
     * [delType description]
     * @param  [type] $userId  [description]
     * @param  [type] $reqData [description]
     * @return [type]          [description]
     */
    public function delType($userId,$id){
        try{
            $object = Db::table("question_type")
                ->where("id",$id)
                ->delete();
            return 0;
        }
        catch(\Exception $e){
            return 3009;
        }
    }  
}